<?php

namespace App\Miya\Middlewares;

use Closure;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class RegularClearCache
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $this->clearByPeriod();
        $this->clearByCacheSize();

        return $next($request);
    }

    //触发缓存大小超出 清醒缓存清理 每隔一小时检测一次
    protected function clearByCacheSize()
    {
        $refresh_ttl = config('allow.refresh_ttl');
        $cache_key = 'regular_clear_cache_by_size';
        if (!cache()->has($cache_key)) {
            $keys = [
                'note.balance' => ['value' => null, 'ttl' => 0],
                'oms_user' => ['value' => null, 'ttl' => $refresh_ttl],
                'oms_order' => ['value' => null, 'ttl' => $refresh_ttl],
                'oms_black_list' => ['value' => null, 'ttl' => $refresh_ttl],
            ];
            foreach ($keys as $key => $value) {
                $keys[$key]['value'] = cache()->get($key);
            }
            $size = $this->getCacheFileSize();
            $threshold = config('miya.regular_clear_cache_size') * 1024;
            if ($size > $threshold) {
                $this->clearCache();
            }

            cache()->set($cache_key, 1, 60 * 60);
            foreach ($keys as $key => $value) {
                cache()->set($key, $value['value'], $value['ttl']);
            }
        }
    }

    //触发周期性缓存星厨
    protected function clearByPeriod()
    {
        $refresh_ttl = config('allow.refresh_ttl');
        $cache_key = 'regular_clear_cache_ok';
        if (!cache()->has($cache_key)) {
            $keys = [
                'note.balance' => ['value' => null, 'ttl' => 0],
                'oms_user' => ['value' => null, 'ttl' => $refresh_ttl],
                'oms_order' => ['value' => null, 'ttl' => $refresh_ttl],
                'oms_black_list' => ['value' => null, 'ttl' => $refresh_ttl],
            ];
            foreach ($keys as $key => $value) {
                $keys[$key]['value'] = cache()->get($key);
            }
            $this->clearCache();

            cache()->set($cache_key, 1, 60 * 60 * config('miya.regular_clear_cache_period'));
            foreach ($keys as $key => $value) {
                cache()->set($key, $value['value'], $value['ttl']);
            }
        }
    }

    //得到所有缓存文件大小
    protected function getCacheFileSize()
    {
        $files = Storage::disk('cache_dir')->allFiles();
        $size = 0;
        foreach ($files as $file) {
            $size += filesize(storage_path('framework/cache/data/' . $file));
        }

        return $size / 1024;
    }

    // 缓存清理规则
    protected function clearCache()
    {
        $refresh_ttl = config('allow.refresh_ttl');

        //某些缓存是不能清除或者不用清理的  比如短信阀值 或者 订单信息 技术人员信息
        $keys = [
            'note.balance' => ['value' => null, 'ttl' => 0],
            'oms_user' => ['value' => null, 'ttl' => $refresh_ttl],
            'oms_order' => ['value' => null, 'ttl' => $refresh_ttl],
            'oms_black_list' => ['value' => null, 'ttl' => $refresh_ttl],
        ];
        foreach ($keys as $key => $value) {
            $keys[$key]['value'] = cache()->get($key);
        }

        cache()->flush();

        foreach ($keys as $key => $value) {
            cache()->set($key, $value['value'], $value['ttl']);
        }
    }
}
